import zhCN from './zh-CN'
import enUS from './en-US'
import jaJP from './ja-JP'
import { string } from 'vue-types'
import { cloneDeep } from 'lodash'
interface LangMap {
	[propName: string]: any
}
interface iTransObj {
	[propName: string]: any
}
enum LocaleEnum {
	zh = 'zh-CN',
	en = 'en-US',
	ja = 'ja-JP',
}
const localeData = localStorage.getItem('locale') || LocaleEnum.zh
const locale: string = localeData.substr(0, 2)

const langData: LangMap = {
	zh: zhCN,
	en: enUS,
	ja: jaJP,
}
//当前语言环境的对象
export const t = langData[locale]
//移动端国际化转化方法
export const iTrans = (text_key: string, obj: iTransObj) => {
	if (locale == 'zh') return ''
	const keys: string[] = text_key.split('.')
	let target: any = cloneDeep(t)
	for (const item of keys) {
		target = target[item] || null
	}
	for (const key of Object.keys(obj)) {
		target =
			target && typeof target == 'string'
				? target.replace(`{${key}}`, obj[key])
				: ''
	}
	return target
}
